import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



public class test3 {
	private static final String LOG_TAG = "ExampleApp";

    private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";

    private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
    private static final String TYPE_DETAILS = "/details";
    private static final String TYPE_SEARCH = "/search";

    private static final String OUT_JSON = "/json";
    private String keyword = "food";
    private HttpURLConnection conn = null;
    private String pagetoken = "";
    private boolean isHaveNext = true;
    // KEY!
    private static final String API_KEY = "AIzaSyB8QPf2KSpG_El4HN8IrvJ3GsT5YBsnrd8";
    
	public test3() {
		//this.getDate();
//		String str = "{'chia':{'num_initX':'16','num_Y_constant':'450','num_Y_rate':'186','num_X_rate':'49','text_X':'480','text_Y_plus':'155','doub_Y':'1390','doub1_X_constant':'68','doub1_X_rate':'49','doub2_X_constant':'569','doub2_X_rate':'10'},'lotto':{'num_initX':'14','num_Y_constant':'263','num_Y_rate':'231','num_X_rate':'51','text_X':'420','text_Y_plus':'198','doub_Y':'1430','doub1_X_constant':'68','doub1_X_rate':'51','doub2_X_constant':'526','doub2_X_rate':'10'},'suplotto':{'doub1_X_constant':'17','doub1_X_rate':'50','doub_Y':'1430','doub2_X_constant':'470','doub2_X_rate':'11','num_initX_constant':'18','num_initX_rate':'288','num_X_rate':'49','num_Y1':'330','num_Y2':'888','num21_X_constant':'64','num21_X_rate':'288','num21_Y':'642','num22_X_constant':'64','num22_X_rate':'293','num22_Y':'1202','num2_X_rate':'49','text_X_plus':'100','text_Y_plus':'377'}}";
//		try {
//			JSONObject jo =  new JSONObject(str);
//			JSONObject chia = jo.getJSONObject("chia");
//			
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		//new Thread(r).start();
		
		String zhu = "6";
		System.out.println(Double.parseDouble(zhu)/(double)4);
	}
	
	private void getDate(){

            
            
	}
	
	Runnable r=new Runnable(){
		@Override
		public void run() {
while(isHaveNext){
    			
    			StringBuilder jsonResults = new StringBuilder();
    	        try {
    	        	String str="https://maps.googleapis.com/maps/api/place/search/json?language=zh-tw&sensor=true&keyword=%E7%BE%8E%E9%A3%9F&location=25.058928,121.532505&radius=500&key="+API_KEY;
    	            if(!pagetoken.equals(""))str+="&pagetoken=" + pagetoken;
    	            isHaveNext = false;
    	            
    	
    	            URL url = new URL(str);
    	            //System.out.println(str);
    	            conn = (HttpURLConnection) url.openConnection();
    	            InputStreamReader in = new InputStreamReader(conn.getInputStream());
    	
    	            int read;
    	            char[] buff = new char[1024];
    	            while ((read = in.read(buff)) != -1) {
    	                jsonResults.append(buff, 0, read);
    	            }
    	            //System.out.println(jsonResults);
    	            try {
    	                JSONObject jsonObj = new JSONObject(jsonResults.toString());
    	                JSONArray predsJsonArray = jsonObj.getJSONArray("results");
    	                
    	                for (int i = 0; i < predsJsonArray.length(); i++) {
    	                	String name = predsJsonArray.getJSONObject(i).getString("name");
    	                	System.out.println(name);
    	                }
    	                String status=jsonObj.getString("status");
    	                if(status.equals("INVALID_REQUEST")){
    	                	isHaveNext=true;
    	                }else{
	    	                try{
	    	                	pagetoken = jsonObj.getString("next_page_token");
	    	                	isHaveNext = true;
	    	                	System.out.println("有token");
	    	                	System.out.println(new SimpleDateFormat("HH:mm:ss").format(new Date()));
	    	                }catch(Exception e1){
	    	                	pagetoken = "";
	    	                	isHaveNext = false;
	    	                	System.out.println("沒token了");
	    	                	System.out.println(new SimpleDateFormat("HH:mm:ss").format(new Date()));
	    	                }
    	                }
     
    	            } catch (JSONException e) {
    	               
    	            }        
    	        
    	        } catch (MalformedURLException e) {
    	
    	        } catch (IOException e) {
    	
    	        } finally {
    	            if (conn != null) {
    	                conn.disconnect();
    	            }
    	        }
    	        
    		}
		}		
	};

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new test3();
	}

}
